-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.c
More file actions
52 lines (44 loc) · 1.15 KB
/
Solution.c
File metadata and controls
52 lines (44 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <string.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
void findLCS(char* str1, char* str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
int dp[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
for (int j = 0; j <= len2; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (str1[i - 1] == str2[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
int index = dp[len1][len2];
char lcs[index + 1];
lcs[index] = '\0';
int i = len1, j = len2;
while (i > 0 && j > 0) {
if (str1[i - 1] == str2[j - 1]) {
lcs[--index] = str1[i - 1];
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1])
i--;
else
j--;
}
printf("Longest Common Subsequence: %s\n", lcs);
}
int main() {
char str1[100], str2[100];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
findLCS(str1, str2);
return 0;
}